<?php
namespace Common\Controller;
use Think\Controller;
class CommonController extends Controller {
  protected function _initialize(){
    //监控url--star
      $url=$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
      Monitor($url);
    //监控url--end
      $uid=$_SESSION['uid'];
      if(!empty($uid)){
        $whereid['id']=$uid;
            $userinfo=M('user')->where($whereid)->field('username,tel,thr_name,head,createtime')->find();
            $this->assign('registertime',$userinfo['createtime']);//注册时间
            if(!empty($userinfo['username'])){
              $this->assign('user',$userinfo['username']);
            }else if(!empty($userinfo['tel'])){ 
              $this->assign('user',$userinfo['tel']);
            }else if(!empty($userinfo['thr_name'])){
              $this->assign('user',$userinfo['thr_name']);
            }
            if(empty($userinfo['head'])){
               $headdefault['head']='/Uploads/headdefault/headdefault'.rand(1,20).'.jpg';
               $uiduser['id']=$uid;
               M('user')->where($uiduser)->save($headdefault);
               $this->assign('head',$headdefault['head']);
            }else{
               $this->assign('head',$userinfo['head']);
            }
      }     $upinfo=M('upinfo')->select();
            $syinfor=M('fbsongyang')->limit(12)->order('sy_id desc')->select();
            $this->assign('upurl',$upinfo[0]['appup_upload_url']);
            $this->assign('syinfors',$this->gitpic());
            $this->assign('uid',$_SESSION['uid']);
            $weekarray=array("日","一","二","三","四","五","六");
            $this->assign('time',time());
            $this->assign('timew',$weekarray[date("w",time())]);
            $this->assign('cityname',$this->getcity());
  }
    




  public function gitpic(){
      $where['sy_type']=1;
      $syinfor=M('fbsongyang')->where($where)->limit(12)->order('sy_id desc')->select();
      foreach ($syinfor as $key => $value) {
            $pic=explode("|",$value['sy_pic_min_min']);
            $syinfors[$key]['sy_pic']=$pic[0];
            $syinfors[$key]['sy_id']=$value['sy_id'];
        }
     return $syinfors;
  }
  public function _empty(){
    header('HTTP/1.1 404 Not Found'); 
    $this->display( 'Public:404' );
  }
  /**
   * 根据IP获取当前城市
   */
  public function getcity(){
      $ip='122.68.39.9';
      $content = file_get_contents("http://api.map.baidu.com/location/ip?ak=3j2s1pCFpoATNL5tcr014rqZbDV8EUh6&ip={$ip}&coor=bd09ll");
      $city_info = json_decode($content);
      $city_name=$city_info->{'content'}->{'address_detail'}->{'city'};
      return $city_name;
  }
}